package com.ymttest.database.sqlwapper;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.coupon;
import com.ymttest.database.model.couponbatch;
import com.ymttest.utils.EnvSetup;

public class couponWapper {
	SqlManager manager;

    public couponWapper() {
        manager = new SqlManager("mysqlcoupon" + EnvSetup.getEnv());
    }

    public List<coupon> selectBycouponCode(String couponCode) {
        List<coupon> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectBycouponCode",
                        couponCode);
        manager.commit();
        return result;
    }

    public List<coupon> selectByBatchId(int batchId) {
        List<coupon> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectByBatchId",
                        batchId);
        manager.commit();
        return result;
    }

//    public List<coupon> selectByBatchIdAndUserId(Integer BatchId,int UserId) {
//        Map m = new HashMap();
//        m.put("BatchId", BatchId);
//        m.put("UserId", UserId);
//        List<coupon> result = manager
//                .getSelectResults(
//                        "com.ymttest.database.mapping.couponMapper.selectByBatchIdAndUserId",
//                        m);
//        manager.commit();
//        return result;
//    }
    public void setValidStart(int RecordId, Date ValidStart) {
        Map m = new HashMap();
        m.put("RecordId", RecordId);
        m.put("ValidStart", ValidStart);
        manager.update(
                        "com.ymttest.database.mapping.couponMapper.setValidStart",
                        m);
        manager.commit();
    }

    public void setValidEnd(int RecordId, Date ValidEnd) {
        Map m = new HashMap();
        m.put("RecordId", RecordId);
        m.put("ValidEnd", ValidEnd);
        manager.update(
                        "com.ymttest.database.mapping.couponMapper.setValidEnd",
                        m);
        manager.commit();
    }
    
    public void setValidStartByCouponCode(String couponCode, Date ValidStart) {
        Map m = new HashMap();
        m.put("couponCode", couponCode);
        m.put("ValidStart", ValidStart);
        manager.update(
                        "com.ymttest.database.mapping.couponMapper.setValidStartByCouponCode",
                        m);
        manager.commit();
    }

    public void setValidEndByCouponCode(String couponCode, Date ValidEnd) {
        Map m = new HashMap();
        m.put("couponCode", couponCode);
        m.put("ValidEnd", ValidEnd);
        manager.update(
                        "com.ymttest.database.mapping.couponMapper.setValidEndByCouponCode",
                        m);
        manager.commit();
    }
    public List<Map> selectCouponInfoByCouponCode(String CouponCode) {
        Map m = new HashMap();
        m.put("CouponCode", CouponCode);
        @SuppressWarnings("unchecked")
		List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectCouponInfoByCouponCode",
                        m);
        manager.commit();
        return result;
    }
   
    /**
     * 根据买家id、创建类型、优惠券类型
     * @param CouponCode
     * @return
     */
    public List<Map> selectByBugIdCreateTypeCouponType(Integer UserId,Integer  CreaterType,Integer CouponType) {
        Map m = new HashMap();
        m.put("UserId", UserId);
        m.put("CreaterType", CreaterType);
        m.put("CouponType", CouponType);
        @SuppressWarnings("unchecked")
		List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectByBugIdCreateTypeCouponType",
                        m);
        manager.commit();
        return result;
    }
    public List<coupon> selectByBatchIdAndUserId(Integer BatchId,Integer UserId) {
	   Map m = new HashMap();
	   m.put("BatchId", BatchId);
	   m.put("UserId", UserId);
        List<coupon> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectByBatchIdAndUserId",
                        m);
        return result;
    }
    /**
     * 根据couponId 查询优惠券批次和优惠券
     * @param CouponId
     * @return
     */
    public List<Map> selectCouponInfoByBatchCode(String CouponCode) {
        @SuppressWarnings("unchecked")
		List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectCouponInfoByBatchCode",
                        CouponCode);
        manager.commit();
        return result;
    }
    /**
     * AcquireMethod 1:直接领取(默认)  2:兑换 3:店铺分享券
     * @param 
     * @return
     */
    public List<Map> selectCouponInfoByAcquireMethod(Integer AcquireMethod) {
        @SuppressWarnings("unchecked")
		List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectCouponInfoByAcquireMethod",
                        AcquireMethod);
        manager.commit();
        return result;
    }
    /**
     * 根据订单id、优惠券code查询优惠券信息
     * @param MainOrderId
     * @param CouponCode
     * @return
     */
    public List<coupon> selectByOrderIdAndCouponCode(Integer MainOrderId,String CouponCode) {
    	Map m=new HashMap<String,Object>();
    	m.put("MainOrderId", MainOrderId);
    	m.put("CouponCode", CouponCode);
		List<coupon> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectByOrderIdAndCouponCode",
                        m);
        manager.commit();
        return result;
    }
    
    /**
     * 根据过期时间查询
     * @param MainOrderId
     * @param CouponCode
     * @return
     */
    public List<coupon> selectByValidEnd(String ValidEnd) {
    	Map m=new HashMap<String,Object>();
    	m.put("ValidEnd", ValidEnd);
		List<coupon> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectByValidEnd",
                        m);
        manager.commit();
        return result;
    }
    
    
    
    
    
    
    

    // 根据短码，用户id，查询短码公共优惠券使用记录
    public List<Map> selectYmtCouponPublicUsed(String couponCode, int userId) {
        Map m = new HashMap();
        m.put("couponCode", couponCode);
        m.put("userId", userId);
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectYmtCouponPublicUsed",
                        m);
        manager.commit();
        return result;
    }
    public List<coupon> selectTop5() {
    	List<coupon> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop5",
                        null);
        return result;
    }
    
    
    




    public void setdValidStart(Date date, String sCouponCode) {
        Map m = new HashMap();
        m.put("date", date);
        m.put("sCouponCode", sCouponCode);
        manager.update(
                "com.ymttest.database.mapping.couponMapper.setdValidStart",
                m);
        manager.commit();
    }

    public void setdValidEnd(Date date, String sCouponCode) {
        Map m = new HashMap();
        m.put("date", date);
        m.put("sCouponCode", sCouponCode);
        manager.update(
                "com.ymttest.database.mapping.couponMapper.setdValidEnd",
                m);
        manager.commit();
    }

    public YmtCoupon selectTop1() {
        YmtCoupon result = (YmtCoupon) manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1",
                        null).get(0);
        return result;
    }
    


    public List<Map> selectTop1InValidPlatformShortCodeCouponInfo() {
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1InValidPlatformShortCodeCouponInfo",
                        null);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1InValidPlatformLinkCouponInfo(int userId) {
        Map m = new HashMap();
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1InValidPlatformLinkCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1InValidSellerCouponInfo(int sellerId, int userId) {
        Map m = new HashMap();
        m.put("sellerId", sellerId);
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1InValidSellerCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ExpirePlatformShortCodeCouponInfo() {
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1ExpirePlatformShortCodeCouponInfo",
                        null);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ExpirePlatFormLinkCouponInfo(int userId) {
        Map m = new HashMap();
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1ExpirePlatFormLinkCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ExpireSellerCouponInfo(int sellerId, int userId) {
        Map m = new HashMap();
        m.put("sellerId", sellerId);
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1ExpireSellerCouponInfo",
                        m);        
        manager.commit();
        return result;
    }

    public List<Map> selectTop1NotStartSellerCouponInfo(int sellerId, int userId) {
        Map m = new HashMap();
        m.put("sellerId", sellerId);
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1NotStartSellerCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1NotStartPlatFormShortCodeCouponInfo() {
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1NotStartPlatFormShortCodeCouponInfo",
                        null);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1NotStartPlatformLinkCouponInfo(int userId) {
        Map m = new HashMap();
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1NotStartPlatformLinkCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ValidPlatformShortCodeCouponInfo() {
        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1ValidPlatformShortCodeCouponInfo",
                        null);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ValidPlatformLinkCouponInfo(int userId) {
        Map m = new HashMap();
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1ValidPlatformLinkCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public List<Map> selectTop1ValidSellerCouponInfo(int sellerId, int userId) {
        Map m = new HashMap();
        m.put("sellerId", sellerId);
        m.put("userId", userId);

        List<Map> result = manager
                .getSelectResults(
                        "com.ymttest.database.mapping.couponMapper.selectTop1ValidSellerCouponInfo",
                        m);
        manager.commit();
        return result;
    }

    public int useCoupon(int userId, String couponCode, Integer mainOrderId) {
        Map m = new HashMap();
        m.put("couponCode", couponCode);
        m.put("userId", userId);
        m.put("mainOrderId", mainOrderId);
        Integer i = manager.session.selectOne("com.ymttest.database.mapping.couponMapper.useCouponV2", m);
        manager.commit();
        return i;
    }

    public static void main(String[] args) {
        YmtCouponWapper test = new YmtCouponWapper();

        System.out.println(test.useCoupon(20238699, "AIL7PT3W1",null));
        // test.insertYmtCouponSetting(3,11);
        // Logger.debug(test.getLastYmtCouponSettingId());


    }
}
